package com.example.hhh.service.impl;

import com.example.hhh.model.Staff;
import com.example.hhh.repository.StaffRepository;
import com.example.hhh.service.StaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class StaffServiceImpl implements StaffService {

    private final StaffRepository staffRepository;

    @Autowired
    public StaffServiceImpl(StaffRepository staffRepository) {
        this.staffRepository = staffRepository;
    }

    @Override
    public Staff createStaff(Staff staff) {
        if (staffRepository.existsByUsername(staff.getUsername())) {
            throw new RuntimeException("Username already exists");
        }
        return staffRepository.save(staff);
    }

    @Override
    public Staff getStaffById(Integer id) {
        return staffRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("Staff not found with id: " + id));
    }

    @Override
    public Staff authenticate(String username, String password) {
        Staff staff = staffRepository.findByUsername(username);
        if (staff != null && staff.getPassword().equals(password)) {
            return staff;
        }
        return null;
    }

    @Override
    public boolean existsByUsername(String username) {
        return staffRepository.existsByUsername(username);
    }
}